package com.kimi.abyss.rbac.service;


import com.kimi.abyss.rbac.api.pojo.SysUser;
import com.kimi.abyss.rbac.domain.User;
import com.kimi.abyss.rbac.domain.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpStatusCodeException;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;

/**
 * no-op
 *
 * @author kimi
 * @date 2021/6/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {

    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    private final UserRepository userRepository;

    public Mono<SysUser> saveUser(SysUser sysUser) {
        User user = new User();
        BeanUtils.copyProperties(sysUser, user);
        user.setPassword(ENCODER.encode(user.getPassword()));
        user.setCreatedDate(LocalDateTime.now());
        return userRepository.save(user).map(u -> u);
    }

    public Mono<User> findByAccount(String account) {

        return userRepository.findByUsername(account);
    }

}
